package com.forum.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.forum.model.entity.Comment;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface CommentMapper extends BaseMapper<Comment> {
    @Select("SELECT * FROM comment WHERE post_id = #{postId} AND parent_id IS NULL ORDER BY created_at DESC")
    List<Comment> selectRootComments(Long postId);

    @Select("SELECT * FROM comment WHERE parent_id = #{parentId} ORDER BY created_at ASC")
    List<Comment> selectChildComments(Long parentId);

    @Update("UPDATE comment SET likes = likes + 1 WHERE id = #{id}")
    int incrementLikes(Long id);

    @Select("SELECT COUNT(*) FROM comment WHERE post_id = #{postId}")
    int countByPostId(Long postId);

    @Select("SELECT username, picture FROM user WHERE id = #{userId}")
    Map<String, Object> selectUserInfo(Long userId);

    @Select("SELECT COUNT(*) FROM comment WHERE post_id = #{postId} AND parent_id IS NULL AND status = 1")
    int countRootComments(Long postId);

    @Select("SELECT COUNT(*) FROM comment WHERE post_id = #{postId} AND parent_id IS NOT NULL AND status = 1")
    int countChildComments(Long postId);
} 